<template>
  <!-- 今日病人 -->
  <section>
    <table width="100%" border="0">
      <tr>
        <td width="460" style="vertical-align: top;padding-top:5px;">
          <el-button-group v-if="statisticArr.length > 0">
            <el-button :type="form.checkStatus === '' ? 'primary' : ''" @click="onGetDataStatus('')">全部</el-button>
            <el-button :type="form.checkStatus === 0 ? 'primary' : ''" @click="onGetDataStatus(0)">待发药({{ statisticArr[0].count }}人)</el-button>
            <el-button :type="form.checkStatus === 1 ? 'primary' : ''" @click="onGetDataStatus(1)">已发药({{ statisticArr[1].count }}人)</el-button>
          </el-button-group>
        </td>
        <td style="text-align:right;">
          <el-form :inline="true" @submit.native.prevent="getData(-1)">
            <!-- <el-form-item>
              <el-select style="width:120px;" v-model="form.payStatus" clearable placeholder="收费状态">
                <el-option v-for="(item,index) in payStatusEnum" :key="index" :value="item.id" :label="item.desc"></el-option>
              </el-select>
            </el-form-item>  -->
            <el-form-item>
              <patientSearch v-model="form.patientId"></patientSearch>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" circle icon="el-icon-search" native-type="submit"> </el-button>
            </el-form-item>
          </el-form>
        </td>
      </tr>
    </table>

    <el-row :gutter="20">
      <el-col v-for="(item, index) in tableList" :key="index" :xs="12" :sm="12" :md="12" :lg="8" :xl="6" class="reserve-user-list">
        <div :class="['card-box', classToName(item.checkStatusId)]">
          <div class="card-nr">
            <div>
              <img v-if="!item.headUrl" src="@/img/unknown-avatar.png" class="head-img" alt="" style="width:70px;height:70px;" />
              <img v-else :src="item.headUrl | imageUrl" class="head-img" alt="" style="width:70px;height:70px;" />
            </div>
            <div style="overflow: hidden;">
              <p class="overflow-omit">
                <span style="font-size:18px;">{{ item.patientName }}</span>
                &nbsp; （{{ item.sexDesc }}&nbsp;<span v-if="item.age > 17">{{ item.age }}岁</span><span v-else>{{ item.birthday | toTimeContrast }}</span
                >）
              </p>
              <div class="overflow-omit">药品：{{ item.checkDesc }}</div>
              <div class="overflow-omit">
                {{ item.checkStatusId | changeAssist(drugStatusEnum) }}&nbsp;/&nbsp;{{ item.payDesc }}&nbsp;/&nbsp;等候{{ item.createTime | computeHour }}
              </div>
              <div class="overflow-omit">开单医生：{{ item.doctorName }}（{{ item.diagnosisTypeDesc }}）</div>
              <div class="overflow-omit">开单时间：{{ item.createTime }}</div>
            </div>
          </div>
          <div class="card-footer">
            <div v-if="item.waitFor" @click="onCheckAffirem(item, 0)">待发药({{ item.waitFor }})</div>
            <!-- <div v-if="item.underWay">检查中({{item.underWay}})</div> -->
            <div v-if="item.execute" @click="onCheckAffirem(item, 1)">已完成({{ item.execute }})</div>
            <div v-if="item.isReturnableMedicine" @click="$refs.refundMedicine.show(item.returnableMedicineList, item)">
              退药({{ item.returnableMedicineList.length }})
            </div>
          </div>
          <!-- <div class="zt-box">{{item.status|changeAssist(reserveTypeArray)}}</div> -->
          <div v-if="item.level" class="vip-box">
            <el-tooltip :content="item.levelName" placement="top">
              <img src="@/img/icon-vip.png" alt="" />
            </el-tooltip>
          </div>
        </div>
      </el-col>
    </el-row>
    <div v-if="tableList.length == 0" style="color:#999;padding:10px 0;text-align:center;">
      <i class="iconfont iconwushuju" style="font-size:40px;"></i>
      <div>暂无数据</div>
    </div>
    <br />
    <pager v-model="pager" @change-pagenum="getData"></pager>
    <RefundMedicine @done="updateRefundableToItem" ref="refundMedicine" />
  </section>
</template>
<script>
import RefundMedicine from "@components/refundMedicine";
import refundableMedicine from "@mixins/refundableMedicine.mixins";
export default {
  props: ["qxBtns"],
  mixins: [refundableMedicine],
  components: {
    RefundMedicine,
    patientSearch: () => import("@/pages/_com/patientSearch.vue")
  },
  data() {
    return {
      form: {
        checkType: 0,
        checkStatus: "",
        payStatus: "",
        queryTime: "",
        patientId: ""
      },
      pager: { pageSize: 12, pageNum: 1, total: 0 },
      tableList: [],
      payStatusEnum: [],
      statisticArr: [],
      drugStatusEnum: []
    };
  },
  async created() {
    let th = this;
    th.getCommonConfigByKey("支付状态").then(res => {
      th.payStatusEnum = res;
    });
    th.getCommonConfigByKey("发药状态").then(res => {
      th.drugStatusEnum = res;
      th.getData();
    });
  },
  methods: {
    //进入药房详情
    onCheckAffirem(obj, status) {
      let th = this;
      th.$router.push({
        path: "patientDrug",
        query: th.getUrlToken({
          cureId: th.jiaMi(obj.cureId),
          status: status,
          patientId: th.jiaMi(obj.patientId)
        })
      });
    },
    //获取统计
    getStatistic() {
      let th = this;
      let j = {};
      Object.assign(j, th.form);
      delete j.checkStatus;
      th.$api.curePrescriptionstatistictype(j).then(res => {
        if (res.executed) {
          th.statisticArr = res.data;
        }
      });
    },
    getData(type) {
      let th = this;
      if (type == -1) {
        th.pager.pageNum = 1;
        th.pager.total = 0;
      }
      let j = {};
      Object.assign(j, th.form, th.pager);
      th.showLoading();
      th.$api.curePrescriptionpageQuery(j).then(async res => {
        if (res.executed) {
          let list = res.dataBody.list;
          th.pager.total = res.dataBody.total;
          // 2021 feat by jy
          // 增加可退费按钮的显示判断
          // checkStatus === 1 为已发药
          if (j.checkStatus === 1) {
            const _list = await this.extraRefundableToList(list);
            // 重新绑定数据 让页面刷新
            // 不可以在上面map生成的promise上面进行异步绑定
            // 会导致页面更新很多次
            list = [..._list];
          }
          this.tableList = list;
        }
        th.showLoading(false);
      });
      th.getStatistic();
    },
    //切换状态
    onGetDataStatus(type) {
      this.form.checkStatus = type;
      this.getData(-1);
    },
    //判断类名
    classToName(type) {
      switch (type) {
        case 0:
          return "bg-dfz";
          break;
        case 1:
          return "bg-yjz";
          break;
        case 4:
          return "bg-sy";
          break;
      }
    }
  }
};
</script>
